<?php
namespace app\controller\admin;

use app\services\admin\ManageService;
use app\services\admin\RoleService;
use app\validate\admin\ManageValidate;
use think\App;
use think\Request;

class Manage extends Application
{
    protected $manageService;

    public function __construct(App $app = null, ManageService $manageService)
    {
        parent::__construct($app);
        $this->manageService = $manageService;
    }

    public function list(Request $request)
    {
        $status = $request->get('status', 0);
        $search = urldecode($request->get('search', ''));

       return view('admin/manage/list', [
            'page_title' => '管理员列表',
            'meta' => $this->manageService->getManagerList($status, $search),
        ]);
    }

    public function edit(Request $request)
    {
        $uid = $request->param('id');
        if ($request->isPost() && $request->isAjax()) {
            $data = $request->post();
            if (! $uid) {
                $result = $this->validate($data, ManageValidate::class . '.create'); //验证类场景
            } else {
                $result = $this->validate($data, ManageValidate::class);
            }

            if (true !== $result) {
                return $this->formatResult(-1, $result, [
                    'token' => $request->token('_token')
                ]);
            }

            if ($uid) {
                list($ret, $msg) = $this->manageService->saveModifyManager($data);
            } else {
                list($ret, $msg) = $this->manageService->saveManager($data);
            }
            if ($ret) {
                return $this->formatResult(0, $msg, ['url' => url('admin/manage/list')]);
            }
            return $this->formatResult(1, $msg, ['token' => $request->token('_token')]);
        }

        $meta['user'] = $this->manageService->getManager($uid);
        $meta['roles'] = app(RoleService::class)->getRolesToSelect(); //获取角色列表

        return view('admin/manage/edit', [
            'page_title' => $uid ? '编辑管理员' : '新增管理员',
            'meta' => $meta,
        ]);
    }

    public function del(Request $request)
    {
        $uid = $request->get('id', '');

        if ($uid && $uid > 1) {
            $res = $this->manageService->delManager($uid);
            if ($res) {
                return $this->formatResult(0, '删除成功');
            }
        }
        //$this->redirect('admin/manage/list');
        return $this->formatResult(1, '删除失败');
    }

    public function password(Request $request)
    {
        if ($request->isAjax() && $request->isPost()) {
            $data = $request->post();
            $result = $this->validate($data, ManageValidate::class . '.password'); //验证类场景

            if (true !== $result) {
                return $this->formatResult(-1, $result, [
                    'token' => $request->token('_token')
                ]);
            }

            $data = $request->only(['old_password', 'password']);
            list($ret, $msg) = $this->manageService->modifyPassword($data);
            if ($ret) {
                return $this->formatResult(0, $msg, ['url' => url('admin/manage/list')]);
            }
            return $this->formatResult(1, $msg, [
                'token' => $request->token('_token')
            ]);
        }

        return view('admin/manage/password', ['page_title' => '修改密码']);
    }
}
